package com.stone.bigdata.cron;

import com.stone.bigdata.dao.fota.ProductListDao;
import com.stone.bigdata.dao.poc.ActiveCityDao;
import com.stone.bigdata.domain.AddCount;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Component
public class CountAddTotal extends QuartzJobBean {

    private ProductListDao productListDao;

    private ActiveCityDao activeCityDao;

    @Autowired
    public CountAddTotal(ProductListDao productListDao, ActiveCityDao activeCityDao) {
        this.productListDao = productListDao;
        this.activeCityDao = activeCityDao;
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext)
            throws JobExecutionException {
        System.out.println("开始执行");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime yesterday = now.minusDays(1);
        // 昨天00:00:00
        Timestamp yesterdayZero = Timestamp.valueOf(LocalDateTime.of(yesterday.toLocalDate(), LocalTime.MIN));
        // 昨天23:59:59
        Timestamp yesterdayEnd = Timestamp.valueOf(LocalDateTime.of(yesterday.toLocalDate(), LocalTime.MAX));
        Integer total = productListDao.getTotalAdd(yesterdayZero, yesterdayEnd);
        if (total != null) {
            System.out.println("总数" + total);
            AddCount addCount = new AddCount();
            addCount.setTotal(total);
            addCount.setCountDate(Date.valueOf(yesterday.toLocalDate()));
            activeCityDao.saveAddTotal(addCount);
        }
    }
}
